﻿using TraumaCenterSystem.Core;
using TraumaCenterSystem.ServiceReference1;
using TraumaCenterSystem.Utils;
using System;
using System.Web;

namespace TraumaCenterSystem.Bll
{
    public class LoginManager : BaseManager
    {
        private const string DESKEY = "12345678";

        public readonly static string CookieNamePrefix = "authcookie";

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        public static OperateResult Login(string userName, string userPassword)
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOfLoginuser result = service.getLogin(userName, userPassword, "");
                if (result.S)
                {
                    Loginuser loginUser = result.D;
                    SysLoginUser sysLoginUser = new SysLoginUser();
                    sysLoginUser.Id = loginUser.Id;
                    sysLoginUser.Personnel_id = loginUser.Personnel_id;
                    sysLoginUser.Name = loginUser.Name;
                    sysLoginUser.DepartCode = loginUser.DepartCode;
                    sysLoginUser.DepartName = loginUser.DepartName;
                    sysLoginUser.UserType = loginUser.UserType;
                    sysLoginUser.ret = loginUser.ret;
                    sysLoginUser.JobTitle = loginUser.JobTitle;
                    sysLoginUser.JobCode = loginUser.JobCode;
                    sysLoginUser.PositionTitle = loginUser.PositionTitle;
                    sysLoginUser.PositionCode = loginUser.PositionCode;
                    sysLoginUser.CurrDepCode = loginUser.DepartCode.Split('&')[0];
                    sysLoginUser.CurrDepName = loginUser.DepartName.Split('&')[0];

                    if (string.IsNullOrWhiteSpace(loginUser.Personnel_id))
                    {
                        resultInfo.Result = 0;
                    }
                    if (resultInfo.Result == 1)
                    {
                        SetLoginCookie(sysLoginUser);
                    }
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return resultInfo;
        }

        /// <summary>
        /// 从cookie读取用户信息
        /// </summary>
        /// <param name="cookieName"></param>
        private static SysLoginUser BuildUser(string keyName)
        {
            var cookie = HttpContext.Current.Request.Cookies[keyName];
            if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
            {
                try
                {
                    var json = StringHelper.DecryptString(cookie.Value, DESKEY);
                    var loginUser = JsonHelper.DeserializeObject<SysLoginUser>(json);
                    if (loginUser != null)
                    {
                        return loginUser;
                    }
                }
                catch
                {
                    //do nothing
                }
            }
            return null;
        }

        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <returns></returns>
        public static SysLoginUser GetLoginUser()
        {
            var keyName = CookieNamePrefix;
            if (!HttpContext.Current.Items.Contains(keyName))
            {
                var user = BuildUser(keyName);
                HttpContext.Current.Items[keyName] = user;
                return user;
            }
            else
            {
                return HttpContext.Current.Items[keyName] as SysLoginUser;
            }
        }

        /// <summary>
        /// 退出cookie登录
        /// </summary>
        public static void Logout()
        {
            var keyName = CookieNamePrefix;
            HttpCookie cookie = new HttpCookie(keyName, string.Empty);
            cookie.Expires = DateTime.Now.AddMonths(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 设置登录Cookie
        /// </summary>
        /// <param name="loginUser"></param>
        public static void SetLoginCookie(SysLoginUser loginUser)
        {
            var keyName = CookieNamePrefix;
            var value = StringHelper.EncryptString(JsonHelper.JsonDate(loginUser), DESKEY);
            HttpCookie cookie = new HttpCookie(keyName, value);
            cookie.Expires = DateTime.Now.AddDays(1);
            HttpContext.Current.Response.Cookies.Add(cookie);
            HttpContext.Current.Items[keyName] = loginUser;
        }
    }
}

public partial class SysLoginUser : Loginuser
{
    public int IsWinform { get; set; }

    public string CurrDepCode { get; set; }

    public string CurrDepName { get; set; }
}

